package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.border.EtchedBorder;

import Controller.DB;
import Model.Customer;
import Model.MenuItem;

public class ItemDetailPopUp extends JFrame {

	private JPanel contentPane;
	private JTextArea description;
	private MenuItem item;

	/**
	 * Create the frame.
	 */
	public ItemDetailPopUp(final Customer cust, final MenuItem itm) {
		item = itm;
		
		setTitle("Detail");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 458, 502);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel itemImage = new JLabel();
		itemImage.setBounds(42, 39, 170, 81);
		itemImage.setIcon(new ImageIcon("src/images/itemPic.png"));
		panel.add(itemImage);
		
		JLabel item_name = new JLabel(item.getName());
		item_name.setBounds(199, 39, 224, 16);
		panel.add(item_name);
		
		description = new JTextArea(item.getDescription());
		description.setEditable(false);
		description.setBackground(this.getBackground());
		description.setBounds(42, 132, 254, 104);
		description.setLineWrap(true);
		description.setWrapStyleWord(true);
		
		panel.add(description);
		description.setColumns(10);
		
		JLabel avg_rating = new JLabel("New label");
		avg_rating.setBounds(199, 104, 224, 16);
		panel.add(avg_rating);
		
		JButton addFavorite = new JButton("Add to Favorites");
		addFavorite.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String sql = "INSERT INTO _445team11.Favorites VALUES ('" + cust.getUserID() + "','" + itm.getItemID() + "')";
		    	try {
					Statement s = DB.conn.createStatement();
					s.executeUpdate(sql);
					JOptionPane.showMessageDialog(null, "Item added to favoites");
				} catch (SQLException sqe) {
					JOptionPane.showMessageDialog(null, "This item is already in your favorites", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		});
		addFavorite.setBounds(300, 207, 134, 29);
		panel.add(addFavorite);
		
		
		JButton review = new JButton("Review Item");
		review.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new ReviewPopUp(cust, item);
			}
			
		});
		review.setBounds(300, 132, 134, 29);
		panel.add(review);
		try {
			
			Statement s = DB.conn.createStatement();

			String sql = "SELECT AVG(reviewRating) FROM _445team11.Review WHERE Item_itemID = '" + item.getItemID() + "'";
			ResultSet rs = s.executeQuery(sql);
			rs.next();
			avg_rating.setText("Average Rating: " + Integer.toString(rs.getInt("AVG(reviewRating)")));
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(40, 265, 394, 176);
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
		scrollPane.setViewportView(panel_1);
		try {
			
			Statement s = DB.conn.createStatement();
			
			
			String sql = "SELECT Customer_userID, reviewRating, reviewComments  FROM _445team11.Review WHERE Item_itemID = '" + item.getItemID() + "'";
			ResultSet rs = s.executeQuery(sql);
			while(rs.next()) {
				JTextArea txt = new JTextArea();
				txt.setEditable(false);
				txt.setBackground(this.getBackground());
				txt.setAlignmentX(CENTER_ALIGNMENT);
				StringBuilder sb = new StringBuilder();
				
				sb.append(String.format("%-30s%10s", rs.getString("Customer_userID"), Integer.toString(rs.getInt("reviewRating"))));
				sb.append("\n");
				String comments = rs.getString("reviewComments");
				if (comments.equals("null")) {
					comments = "";
				}
				sb.append(comments);
				txt.setText(sb.toString());
				panel_1.add(Box.createVerticalStrut(10));
				panel_1.add(txt);
				panel_1.add(new JSeparator());
			}
			scrollPane.validate();
			panel.add(scrollPane);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}
